package com.swind.fast.modules.app.halali.partner.controller;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swind.fast.common.api.vo.Result;
import com.swind.fast.common.system.query.QueryGenerator;
import com.swind.fast.common.util.oConvertUtils;
import com.swind.fast.common.constant.CommonConstant;
import com.swind.fast.modules.app.halali.partner.entity.HalaliUser;
import com.swind.fast.modules.app.halali.partner.service.IHalaliUserService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import com.swind.fast.common.system.util.JwtUtil;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.swind.fast.common.system.base.controller.SwindController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import com.swind.fast.common.util.RedisUtil;
import org.springframework.context.annotation.Lazy;

// import com.swind.fast.common.aspect.annotation.AutoLog;

import com.swind.fast.modules.system.entity.SysAnnouncement;
import com.swind.fast.modules.system.entity.SysAnnouncementSend;
import com.swind.fast.modules.system.service.ISysAnnouncementSendService;
import com.swind.fast.modules.system.service.ISysAnnouncementService;

import com.swind.fast.modules.app.halali.partner.vo.request.LoginVo;
import com.swind.fast.modules.app.halali.partner.vo.response.UserInfoVo;

 /**
 * @Description: 餐厅
 * @Author: swind
 * @Date:   2022-01-20
 * @Version: V1.0
 */
// @Api(tags="商户端-用户管理")
@RestController
// @RequestMapping("/app/user")
@Slf4j
public class HalaliUserController extends SwindController<HalaliUser, IHalaliUserService> {

    @Lazy
    @Resource
    private RedisUtil redisUtil;

	// @ApiOperation(value="用户-登录", notes="")
	@PostMapping(value = "/login")
	public Result<UserInfoVo> loginAction(@RequestBody LoginVo loginVo) {
		String mobile = loginVo.getMobile();
		String code = loginVo.getCode();
        if (oConvertUtils.isEmpty(code) || !code.equals("666666")) {
        	return Result.errorT("Code Error！");
        }
        if (oConvertUtils.isEmpty(mobile)) {
        	return Result.errorT("Phone number can't be empty!");
        }
		UserInfoVo result = service.login(loginVo);
		return Result.OK("OK", result);
	}

    // @ApiOperation(value="用户-退出登录", notes="")
    @PostMapping(value = "/logout")
    public Result<Object> logout(HttpServletRequest request,HttpServletResponse response) {
        //用户退出逻辑
        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        if (oConvertUtils.isEmpty(token)) {
            return Result.error("退出登录失败！");
        }
        String mobile = JwtUtil.getUsername(token);
        HalaliUser sysUser = service.getUserByMobile(mobile);
        if (sysUser != null) {
            log.info("用户名:  " + sysUser.getMobile() + ",退出成功！ ");
            //清空用户登录Token缓存
            redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token);
            //调用shiro的logout
            SecurityUtils.getSubject().logout();
            return Result.ok("退出登录成功！");
        } else {
            return Result.error("Token无效!");
        }
    }

}
